package com.qyhl.webtv.module_news.news.smallvideo;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.module_news.news.smallvideo.fragment.SmallVideoFragment;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by qyhl on 2017/11/15.
 * 小视频
 */

@Route(path = ARouterPathConstant.SMALLVIDEO)
public class SmallVideoActivity extends BaseActivity implements SmallVideoContract.SmallVideoView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.video_full_container)
    FrameLayout videoFullContainer;
    @BindView(R2.id.back_btn)
    ImageView backBtn;

    private SmallVideoPresenter mPresenter;

    @Autowired(name = "title")
    String title;
    @Autowired(name = "index")
    String mIndex = "0";
    private List<Fragment> mFragmentList;
    private SmallVideoPageAdapter mPageAdapter;
    private List<SectionsData> mList;
    private SmallVideoFragment fragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected int getContentView() {
        return R.layout.news_activity_news_smallvideo;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new SmallVideoPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {

    }

    /**
     * 初始化
     */
    private void initView() {
        mTitle.setText(StringUtils.isEmpty(title) ? "小视频" : title);
        initData();
        mFragmentList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            fragment = SmallVideoFragment.newInstance(mList.get(i).getSectionId());
            mFragmentList.add(fragment);
        }
        mPageAdapter = new SmallVideoPageAdapter(getSupportFragmentManager(), mFragmentList);
        mPageAdapter.setData(mList);
        viewPager.setAdapter(mPageAdapter);
        viewPager.setOffscreenPageLimit(6);
        try {
            Class<? extends SlidingTabLayout> aClass = tabLayout.getClass();
            Field f = aClass.getDeclaredField("mTabPadding");
            f.setAccessible(true);
            f.set(tabLayout, StringUtils.dip2px(this, 15));
        } catch (Exception e) {
            e.printStackTrace();
        }
        tabLayout.setViewPager(viewPager);
        tabLayout.setTextSelectColor(HomeThemeColorUtil.getCheckColor());
        tabLayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        tabLayout.getTitleView(0).getPaint().setFakeBoldText(true);
        viewPager.setCurrentItem(Integer.parseInt(mIndex));

        backBtn.setOnClickListener(v -> finish());
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mList = new ArrayList<>();
        mList.add(new SectionsData("0", "最新"));
        mList.add(new SectionsData("1000006", "搞笑"));
        mList.add(new SectionsData("1000007", "萌物"));
        mList.add(new SectionsData("1000008", "运动"));
        mList.add(new SectionsData("1000009", "娱乐"));
        mList.add(new SectionsData("1000010", "现场"));
    }


    //小视频自定义数据模型
    class SectionsData implements Serializable {

        SectionsData(String sectionId, String title) {
            this.sectionId = sectionId;
            this.title = title;
        }

        public String sectionId;
        public String title;

        public String getSectionId() {
            return sectionId;
        }

        public void setSectionId(String sectionId) {
            this.sectionId = sectionId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("小视频首页");
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("小视频首页");
        MobclickAgent.onPause(this);
    }

    @Override
    public void onBackPressed() {
        if (fragment.onBackPressed()) {
            return;
        } else {
            finish();
        }
    }
}